      SUBROUTINE HB(IFILE, NtFileName)

      IMPLICIT NONE

      INCLUDE 'hbcomm.inc'
      INCLUDE 'hbtype.inc'

      INTEGER I,J,K,JJ,UN,IERROR,IFILE,NLOOP
      INTEGER NOENT, OFFSET, NABVOFF, NABVCUT

      INTEGER sig1p, sig2p, hsb1p,hsb2p,lsb1p,lsb2p;
      INTEGER,SAVE :: BOOKED = 0

      REAL DDD

c     REAL ax1, ax2, st1, st2

      PARAMETER (UN=100)

      EQUIVALENCE (ntr,VEC1(1))
c     CHARACTER*120 CHFIN(0:2000)
      CHARACTER*1024 NtFileName

      REAL VEC(12), VEC2(8), VEC3(2)


c     offset=0

c     ax1=10
c     ax2=10
c     st1=10
c     st2=10

      IF (BOOKED .eq. 0) THEN
         DO K=0, 5000000, 500000
            IF (K.gt.0.and.K.lt.1499999) THEN
               CYCLE
            ENDIF

            DO J=0,20000,10000
               DO I=0,6000,1000
                  offset = I+J
                  CALL HBBOOK(offset, K)
               ENDDO
            ENDDO
         ENDDO
         BOOKED=1
      ENDIF
c     IF(IFILE.EQ.0) THEN
c     offset=10000
c     CALL HBBOOK(offset)
c     ENDIF

      CALL HNOENT(1,NLOOP)
      PRINT *,'-------------------------------------------------------'
      PRINT *,NLOOP,' Entries in the hb ntuple'
      PRINT *,'IN THE FILE # ',IFILE,'--',NtFileName(1:LEN_TRIM(NtFileName))
      PRINT *,'-------------------------------------------------------'
      CALL HGNPAR(1,' ')

c     write(6,*) 1

      IF (NLOOP .LT. 1000) RETURN


      DO I=1,NLOOP

c     write(6,*) 2

         CALL HGNF(1,I,VEC1,IERROR)

c     roman cuts

c     write(6,*) 1

         IF(ABS(dr1).LT.0.2.AND.ABS(dr2).LT.0.2) THEN
            IF(ABS(dz1).LT.3.AND.ABS(dz2).LT.3) THEN
c     IF (evtr2<0.3) THEN
c     IF (evtr2<0.32) THEN
               IF (EID1<0.9.and.EID2<0.9) THEN
                  IF (kprobpi1<0.9.and.kprobpi2<0.9) THEN
                     IF (pprobpi1<0.9.and.pprobpi2<0.9) THEN
                        IF (ABS(recmpi1-10.63)<0.04 .or. ABS(recmpi2-10.63)<0.04) THEN
                           IF (M2MU.eq.-1.0) then ! .and. PROB2PI > 1E-6) THEN
                              
                              CALL HBFILL1(0)

                              if (nabv15.gt.1) THEN
                                 CALL HBFILL1(1500000)
                              ENDIF
                              
                              if (nabv20.gt.1) THEN
                                 CALL HBFILL1(2000000)
                              ENDIF

                              if (nabv25.gt.1) THEN
                                 CALL HBFILL1(2500000)
                              ENDIF

                              if (nabv30.gt.1) THEN
                                 CALL HBFILL1(3000000)
                              ENDIF

                              if (nabv35.gt.1) THEN
                                 CALL HBFILL1(3500000)
                              ENDIF

                              if (nabv40.gt.1) THEN
                                 CALL HBFILL1(4000000)
                              ENDIF

                              if (nabv45.gt.1) THEN
                                 CALL HBFILL1(4500000)
                              ENDIF

                              if (nabv50.gt.1) THEN
                                 CALL HBFILL1(5000000)
                              ENDIF

                           ENDIF


c     IF (M2MU.eq.0 .and. PROB2PI > 1E-6) THEN
c     IF (M2MU.eq.0
c     $     .and.recm2pi.gt.9.7
c     $     .and.recm2pi.lt.10.5
c     $     .and.egboost.gt.0.1
c     $     .and.egboost.lt.1.1) THEN

                           IF (M2MU.ne.0
     $                          .or.recm2pi.lt.9.7
     $                          .or.recm2pi.gt.10.5
     $                          .or.egboost.lt.0.1
     $                          .or.egboost.gt.1.1) THEN
                              CYCLE
                           ENDIF

                           
                           CALL HBFILL2(0)

                          if (nabv15.gt.1) THEN
                              CALL HBFILL2(1500000)
                           ENDIF
                           
                           if (nabv20.gt.1) THEN
                              CALL HBFILL2(2000000)
                           ENDIF
                           
                           if (nabv25.gt.1) THEN
                              CALL HBFILL2(2500000)
                           ENDIF
                           
                           if (nabv30.gt.1) THEN
                              CALL HBFILL2(3000000)
                           ENDIF
                           
                           if (nabv35.gt.1) THEN
                              CALL HBFILL2(3500000)
                           ENDIF
                           
                           if (nabv40.gt.1) THEN
                              CALL HBFILL2(4000000)
                           ENDIF
                           
                           if (nabv45.gt.1) THEN
                              CALL HBFILL2(4500000)
                           ENDIF
                           
                           if (nabv50.gt.1) THEN
                              CALL HBFILL2(5000000)
                           ENDIF
                        ENDIF
                     ENDIF
                  ENDIF
               ENDIF
            ENDIF
         ENDIF
      ENDDO
    
      RETURN
      END


